import { defineStore } from 'pinia';
import { Response } from '@/entity/response';
import { Menu } from '@/entity/menu';

export const menu = defineStore('menu', {
    state: () => ({
        menus: [] as Menu[],
    }),
    getters: {},
    actions: {
        setMenus(menus: any) {
            if (menus) {
                this.menus = menus;
            }
        },
        getMenus() {
            const {menuApi} = useApi();
            menuApi.getList().then((res: any) => {
                let { data }: Response = res;
                if (data == null) {
                    return;
                }
                const arr = data;
                let newsList = {
                    menuName: "更多",
                    children: [] as any[]
                };
                let newsMenuList:any = [];
                for (let i = 0; i < arr.length; i++) {
                    if ((i + 1) > 6) {
                        newsList.children.push(arr[i]);
                    } else {
                        newsMenuList.push(arr[i]);
                    }
                }
                if (newsList.children.length > 0) {
                    newsMenuList.push(newsList);
                }
                this.setMenus(newsMenuList);
            });
        }
    },
    persist: true,
})